<?php
namespace Account\Model;

use Zend\Db\Adapter\Adapter;
//use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Select;

class AccountTable extends AbstractTableGateway
{
    protected $table = 'account';
	protected $table1 = 'person';

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
    }

    public function fetchAll()
    {
        $resultSet = $this->select(function (Select $select) {
                    $select->join(array('p'=>'person'),
							'account.account_id = p.id');
									
                });
        $entities = array();
        foreach ($resultSet as $row) {
            $entity = new Entity\Account();
            $entity->setId($row->id)
				   ->setName($row->name)
                   ->setDescription($row->description)
                   ->setEmployees($row->employees)
                   ->setAnnualrevenue($row->annualrevenue)
				   ->setOfficephone($row->officephone)
                   ->setOfficefax($row->officefax)
				   ->setWebsite($row->website)
				   ->setType_Customfield_Id($row->type_customfield_id)
				   ->setAccount_Id($row->account_id);
			$entity2 = new Entity\Person();
			$entity2->setId($row->id)
					->setFirstname($row->firstname);
            $entities[] = $entity;
			$entities2[] = $entity2;
        }
        return $entities;
    }

    public function getAccount($id)
    {
        $id  = (int) $id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }
	
	public function getQuery1() {
		$resultSet = $db->query('Select name, type_customfield_id, p.firstname from account');
		return $resultSet;
	}
	/*
    public function saveAccount(Account $account)
=======
	
  /* public function saveAccount(Account $account)
>>>>>>> .r14
    {
<<<<<<< .mine
        $data = array(
            'id' => $account->id,
            'name'  => $account->name,
			'description' => $account->description,
            'annualrevenue'  => $account->annualrevenue,
			'employees' => $account->employees,
            'officephone'  => $account->officephone,
			'officefax' => $account->officefax,
			'website' => $account->website,
			'ownedsecurableitem_id'  => $account->ownedsecurableitem_id,
			'account_id' => $account->account_id,
            'billingaddress_address_id'  => $account->billingaddress_address_id,
			'industry_customfield_id'  => $account->industry_customfield_id,
			'shippingaddress_id'  => $account->shippingaddress_address_id,
			'type_customfield_id' => $account->type_customfield_id,
		
            
		
        );

        $id = (int)$account->id;
        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
            if ($this->getAccount($id)) {
                $this->tableGateway->update($data, array('id' => $id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }*/
	
    public function deleteAccount($id)
    {
        $this->tableGateway->delete(array('id' => $id));
    }
}
?>